<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('申请审批')"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">设备名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="deviceName" placeholder="" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>
</body>

<script type="text/html" id="user-bar">
    {{#if (d.status == 0) { }}
    <button class="pear-btn pear-btn-primary pear-btn-sm" title="开始维修" lay-event="start"><i
            class="layui-icon layui-icon-util"></i></button>
    {{# } }}
    {{#if (d.status == 1) { }}
    <button class="pear-btn pear-btn-primary pear-btn-sm" title="维修成功" lay-event="pass"><i
            class="layui-icon layui-icon-ok"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" title="维修失败" lay-event="faild"><i
            class="layui-icon layui-icon-close"></i></button>
    {{# } }}
</script>


<script type="text/html" id="examine-type">
    {{#if (d.sort == 0) { }}
    <span>办公类设备</span>
    {{# }else if(d.sort == 1){ }}
    <span>科研类设备</span>
    {{# }else if(d.sort == 2){ }}
    <span>其他类设备</span>
    {{# } }}
</script>


<script type="text/html" id="storage-status">
    {{#if (d.status == 0) { }}
    <a class="pear-btn pear-btn-primary pear-btn-sm" style="cursor:default"> 待 维 修 </a>
    {{# }else if(d.status == 1){ }}
    <a class="pear-btn pear-btn-warming pear-btn-sm" style="cursor:default">
        维 修 中
    </a>
    {{# }else if(d.status == 2){ }}
    <a class="pear-btn pear-btn-success pear-btn-sm" style="cursor:default">
        维修成功
    </a>
    {{# }else if(d.status == 3){ }}
    <a class="pear-btn pear-btn-danger pear-btn-sm" style="cursor:default">
        维修失败
    </a>
    {{# } }}
</script>

<th:block th:include="include :: footer"/>
<script>
    layui.use(['table', 'form', 'jquery', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        let MODULE_PATH = "/device/repair/";

        let cols = [
            [
                {title: '设备名', event: 'setcilck', field: 'deviceName', width: 250},
                {title: '制造公司', event: 'setcilck', field: 'deviceCompany', width: 150},
                {title: '单价', event: 'setcilck', field: 'devicePrice', width: 100},
                {title: '申请时间', event: 'setcilck', field: 'createTime', width: 170},
                {title: '状态', event: 'setcilck', field: 'status', width: 110, templet: '#examine-type'},
                {title: '分类', event: 'setcilck', field: 'sort', width: 110, templet: '#storage-status'},
                {title: '申请理由', event: 'setcilck', field: 'remark', width: 200},
                {title: '操作', toolbar: '#user-bar', align: 'center', width: 130}
            ]
        ]

        table.render({
            elem: '#user-table',
            url: MODULE_PATH + 'examine/data',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });


        //列表点击事件
        table.on('tool(user-table)', function (obj) {
            // 当前行数据 JSON.stringify(data)
            if (obj.event === 'setcilck') {
                layer.open({
                    type: 2,
                    title: '查看申请单',
                    shade: 0.1,
                    area: ['800px', '600px'],
                    content: MODULE_PATH + 'examine/view?id=' + obj.data['id']
                });
                //标注选中样式
                obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            } else if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'pass') {
                window.pass(obj);
            } else if (obj.event === 'start') {
                window.start(obj);
            } else if (obj.event === 'faild') {
                window.faild(obj);
            }
        });


        table.on('toolbar(user-table)', function (obj) {
            if (obj.event === 'refresh') {
                window.refresh();
            }
        });

        form.on('submit(user-query)', function (data) {
            table.reload('user-table', {
                where: data.field
            })
            return false;
        });

        window.start = function (obj) {
            var remark = "";
            layer.prompt({
                formType: 0,
                value: '',
                title: '请输入维修结果',
                btn: ['确定', '取消'], //按钮，
                btnAlign: 'c'
            }, function (value, index) {
                remark = value;
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "examine/start",
                    data: JSON.stringify({
                        remark: remark,
                        id: obj.data['id']
                    }),
                    dataType: 'json',
                    contentType: 'application/json',
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                window.refresh();
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.pass = function (obj) {
            var remark = "";
            layer.prompt({
                formType: 0,
                value: '',
                title: '请输入维修结果',
                btn: ['确定', '取消'], //按钮，
                btnAlign: 'c'
            }, function (value, index) {
                remark = value;
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "examine/pass",
                    data: JSON.stringify({
                        remark: remark,
                        id: obj.data['id']
                    }),
                    dataType: 'json',
                    contentType: 'application/json',
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                window.refresh();
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.failed = function (obj) {
            var remark = "";
            layer.prompt({
                formType: 0,
                value: '',
                title: '请输入维修结果',
                btn: ['确定', '取消'], //按钮，
                btnAlign: 'c'
            }, function (value, index) {
                remark = value;
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "examine/failed",
                    data: JSON.stringify({
                        remark: remark,
                        id: obj.data['id']
                    }),
                    dataType: 'json',
                    contentType: 'application/json',
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                window.refresh();
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }


        window.refresh = function (param) {
            table.reload('user-table');
        }
    })
</script>
</html>